package library.and.xps.com.step.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.logger.Logger;
import com.squareup.okhttp.Request;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import library.and.xps.com.step.App;
import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.ExitTeam;
import library.and.xps.com.step.m.model.JoinedTeam;
import library.and.xps.com.step.m.model.Team;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.view.base.BaseActivity;

public class MyTeamActivity extends BaseActivity implements View.OnClickListener, com.orhanobut.dialogplus.OnClickListener {
    Team team;
    TextView tv_teamName, tv_teamId, tv_org_desc, tv_name, tv_phone, tv_email, actionbar_tv_title, tv_authroity, tv_department;
    ImageView action_bar_left, iv_team_avatar;
    AutoRelativeLayout ar_exit_team, ar_phone, ar_name, ar_authroity, ar_department, ar_team_manage;
    AutoLinearLayout al_detail;
    JoinedTeam joinTeamRes;

    @Override
    protected int getLayout() {
        return R.layout.activity_myteam;
    }

    @Override
    protected void setData() {
        getCurrentTeam();
    }

    void getCurrentTeam() {
        if (getUser() == null || !JUtils.isNetWorkAvilable()) return;

        String data = URLConstants.TEAM.GetMyTeam + "?" + "userid=" + getUser().getUserid();
        OkHttpClientUtils.getDataAsync(App.getInstance().getApplicationContext(), data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                if (e instanceof RuntimeException) {
                    JUtils.Toast(e.getMessage());
                }
                dissmisDialog();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                dissmisDialog();
                if (!HttpResponsHelper.SuccessResponce(response)) {
                    finish();
                    return;
                }
                String json = response.getBody();
                Logger.json(json);
                joinTeamRes = new Gson().fromJson(json, JoinedTeam.class);

                if (joinTeamRes != null && HttpResponsHelper.SuccessResponce(joinTeamRes)) {
                    JUtils.Log("已加入的团队:" + joinTeamRes.toString());
                } else {
                    JUtils.Log("已加入的团队：" + "空");
                }

                if (HttpResponsHelper.SuccessResponce(joinTeamRes)) {

                    realm.beginTransaction();
                    Team team = new Team();
                    team.setAddress(joinTeamRes.getObj().getAddress());
                    team.setCreatenickname(joinTeamRes.getObj().getCreatenickname());
                    team.setCreater(joinTeamRes.getObj().getCreater() + "");
                    team.setCreatetime(joinTeamRes.getObj().getCreatetime() + "");
                    team.setTname(joinTeamRes.getObj().getTname());
                    team.setEnterprise(joinTeamRes.getObj().getEnterprise());
                    team.setEnterpriseid(joinTeamRes.getObj().getEnterpriseid() + "");
                    team.setDescription(joinTeamRes.getObj().getDescription());
                    team.setType(joinTeamRes.getObj().getType() + "");
                    team.setMarker(joinTeamRes.getObj().getMark());
                    team.setEmail(joinTeamRes.getObj().getEmail());
                    team.setTeamId(joinTeamRes.getObj().getTeamid() + "");
                    team.setIntname(joinTeamRes.getObj().getIntname());
                    team.setRole(joinTeamRes.getObj().getRole());
                    if (joinTeamRes.getObj().getIsno() == -1) {
                        team.setPassWordEnable(false);
                    } else if (joinTeamRes.getObj().getIsno() == 0) {
                        team.setPassWordEnable(true);
                    }
                    getUser().getTeams().clear();
                    getUser().getTeams().add(team);
                    setTeamInfo(joinTeamRes);
                    MyTeamActivity.this.team = team;
                    realm.commitTransaction();

                }
            }

            @Override
            public void onStart() {
                showDialogNoCancel();
            }
        }, "");

    }

    void setTeamInfo(JoinedTeam joinTeamRes) {
        tv_teamName.setText(joinTeamRes.getObj().getTname());
        tv_org_desc.setText("团队简介:" + "\n" + joinTeamRes.getObj().getDescription());
        tv_teamId.setText("ID:" + joinTeamRes.getObj().getTeamid());
        tv_phone.setText(getUser().getPhone());
        tv_email.setText(joinTeamRes.getObj().getEmail());
        if (joinTeamRes.getObj().getRole() == 1) {
            tv_authroity.setText("管理员");
        } else {
            tv_authroity.setText("成员");
            ar_team_manage.setVisibility(View.GONE);
        }
        tv_name.setText(joinTeamRes.getObj().getIntname());
        tv_department.setText(joinTeamRes.getObj().getDname());

        CommonUtils.setUpimg(this, iv_team_avatar, joinTeamRes.getObj().getMark(), true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0) {
            getCurrentTeam();
        }
    }

    @Override
    protected void init() {
        actionbar_tv_title = r2v(R.id.actionbar_tv_title);
        action_bar_left = r2v(R.id.actionbar_iv_back);
        action_bar_left.setOnClickListener(this);
        ar_team_manage = r2v(R.id.ar_team_manage);
        ar_team_manage.setOnClickListener(this);
        ar_exit_team = r2v(R.id.ar_exit_team);
        ar_phone = r2v(R.id.ar_phone);
        ar_name = r2v(R.id.ar_name);
        ar_authroity = r2v(R.id.ar_authroity);
        ar_department = r2v(R.id.ar_department);
        iv_team_avatar = r2v(R.id.iv_team_avatar);
        tv_teamName = r2v(R.id.tv_teamName);
        tv_teamId = r2v(R.id.tv_teamId);
        tv_org_desc = r2v(R.id.tv_org_desc);
        tv_name = r2v(R.id.tv_name);
        tv_phone = r2v(R.id.tv_phone);
        tv_email = r2v(R.id.tv_email);
        al_detail = r2v(R.id.al_detail);
        tv_authroity = r2v(R.id.tv_authroity);
        tv_department = r2v(R.id.tv_department);
        al_detail.setOnClickListener(this);
        ar_exit_team.setOnClickListener(this);
        ar_phone.setOnClickListener(this);
        ar_name.setOnClickListener(this);
        ar_authroity.setOnClickListener(this);
        ar_department.setOnClickListener(this);
        actionbar_tv_title.setText("我的团队");
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.ar_department:
                break;
            case R.id.ar_authroity:
                break;
            case R.id.ar_name:
                break;
            case R.id.ar_phone:
                break;
            case R.id.ar_email:

                break;
            case R.id.ar_exit_team:
                CommonUtils.showCompleteDialog(this, this, "确定退出团队?");
                break;
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.al_detail:
                startActivity(new Intent(this, TeamDetailActivity.class));
                break;
            case R.id.ar_team_manage:
                startActivity(new Intent(this, TeamManageActivity.class));
                break;
        }
    }

    void exitThisShitCompany() {
        if (!TeamHelper.getIns().hasTeam()) return;
        String url = URLConstants.TEAM.RemoveTeam + "?" + "userid=" + getUser().getUserid() + "&" + "teamid=" + team.getTId() + "&departmentid=" + joinTeamRes.getObj().getDepartmentid();

        OkHttpClientUtils.getDataAsync(this, url, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                dissmisDialog();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                dissmisDialog();
                ExitTeam exitTeam = new Gson().fromJson(response.getBody(), ExitTeam.class);

                if (HttpResponsHelper.SuccessResponce(exitTeam)) {
                    JUtils.Toast("退出成功!");
                    finish();

                    realm.beginTransaction();
                    getUser().getTeams().clear();
                    realm.commitTransaction();

                }
            }

            @Override
            public void onStart() {
                showDialog();
            }
        }, "");
    }

    @Override
    public void onClick(DialogPlus dialog, View view) {
        switch (view.getId()) {
            case R.id.footer_close_button:
                dialog.dismiss();
                break;
            case R.id.footer_confirm_button:
                exitThisShitCompany();
                dialog.dismiss();
                break;

        }
    }
}
